package com.yjc.rabbitMQ.three;

import com.rabbitmq.client.CancelCallback;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;
import com.yjc.rabbitMQ.utils.RabbitMQUtils;

/**
 * @author shkstart
 * @create 2022-07-30 16:08
 */
public class Worker02 {
    private static final String QUEUE_NAME = "Hello_queue_product *(￣▽￣)";

    public static void main(String[] args) throws Exception {
        Channel channel = RabbitMQUtils.getChannel();
        System.out.println("C1接收消息较短");

        boolean autoACK = false;
        DeliverCallback deliverCallback = (consumerTag, message) -> {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(new String(message.getBody(), "UTF-8"));
            channel.basicAck(message.getEnvelope().getDeliveryTag(), false);
        };

        CancelCallback cancelCallback = (consumerTag) -> {
            System.out.println("消息取消接收");
        };
        //开启不公平分发
//        int prefetchCount = 1;
        //预取值是2
        int prefetchCount = 2;
        channel.basicQos(prefetchCount);
        channel.basicConsume(QUEUE_NAME, autoACK, deliverCallback, cancelCallback);
    }
}
